﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;
using Cms.Web.Framework.Plugins.Attributes;
using iLib.Web.UI.WebControls;

namespace Cms.Web.Framework.Plugins
{
    public class SelectLocale : BaseTemplatedPlugin
    {
        // Property fields
        private string _flagsMediaFolder;
        private LocaleTranslationOption _localeTranslationOption;
        private bool _showCurrentLocale;

        // Template controls
        private iRepeater rpLocales;

        public SelectLocale()
        {
            _showCurrentLocale = true;
            _localeTranslationOption = Domain.LocaleTranslationOption.CurrentLocale;
        }
        
        [Enumeration("LocaleTranslationOption", true)]
        public LocaleTranslationOption LocaleTranslationOption
        {
            get { return _localeTranslationOption; }
            set { _localeTranslationOption = value; }
        }
        
        public bool ShowCurrentLocale
        {
            get { return _showCurrentLocale; }
            set { _showCurrentLocale = value; }
        }

        public string FlagsMediaFolder
        {
            get { return _flagsMediaFolder; }
            set { _flagsMediaFolder = value; }
        }
        
        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                rpLocales = (iRepeater)FindControl("rpLocales");
                rpLocales.ItemDataBound += rpLocales_ItemDataBound;
                ILocaleRepository localeRepository = new LocaleRepository();
                IList<Locale> locales = new List<Locale>();
                for (int i = 0; i < RequestDetails.Site.Locales.Count; i++)
                {
                    if (_showCurrentLocale && RequestDetails.Site.Locales[i] == RequestDetails.Locale)
                        rpLocales.SelectedIndex = i;
                    if (!_showCurrentLocale && RequestDetails.Site.Locales[i] == RequestDetails.Locale)
                        continue;
                    locales.Add(RequestDetails.Site.Locales[i]);
                }
                rpLocales.DataSource = locales;
                rpLocales.DataBind();
            }
            catch (Exception ex)
            {
                Logging.LogError("SelectLocale", ex);
                Controls.Clear();
            }
        }
        
        private void rpLocales_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem &&
                e.Item.ItemType != ListItemType.SelectedItem)
                return;
            var locale = (Locale)e.Item.DataItem;
            var hlLocale = (HyperLink)e.Item.FindControl("hlLocale");
            var litName = (Literal)e.Item.FindControl("litName");
            var litLanguage = (Literal)e.Item.FindControl("litLanguage");
            var imgFlag = (Image)e.Item.FindControl("imgFlag");
            if (litName != null)
            {
                litName.Text = _localeTranslationOption == LocaleTranslationOption.CurrentLocale
                                   ? locale.GetTranslation(RequestDetails.Locale.CultureCode).Name
                                   : locale.GetTranslation(locale.CultureCode).Name;
            }
            if (litLanguage != null)
            {
                litLanguage.Text = _localeTranslationOption == LocaleTranslationOption.CurrentLocale
                                       ? locale.GetTranslation(RequestDetails.Locale.CultureCode).Language
                                       : locale.GetTranslation(locale.CultureCode).Language;
            }
            if (imgFlag != null)
            {
                imgFlag.ImageUrl = "~/" + _flagsMediaFolder + "/" + locale.UrlCode + ".png";
                imgFlag.AlternateText = _localeTranslationOption == LocaleTranslationOption.CurrentLocale
                                            ? locale.GetTranslation(RequestDetails.Locale.CultureCode).Language
                                            : locale.GetTranslation(locale.CultureCode).Language;
            }
            if (hlLocale == null)
                return;
            hlLocale.NavigateUrl = RequestDetails.Protocol + RequestDetails.SiteRootUrl + "/";
            if (locale != RequestDetails.Site.DefaultLocale)
                hlLocale.NavigateUrl += locale.UrlCode + "/";
            hlLocale.NavigateUrl += RequestDetails.Page.GetVirtualPath();
            if (!string.IsNullOrEmpty(RequestDetails.QueryString))
                hlLocale.NavigateUrl += "?" + RequestDetails.QueryString;
        }
    }
}